package cc.javaee.bbs.controller.admin;

import cc.javaee.bbs.constants.Constants;
import cc.javaee.bbs.model.Bankuai;
import cc.javaee.bbs.model.PageBean;
import cc.javaee.bbs.model.User;
import cc.javaee.bbs.model.activity.KbrActivity;
import cc.javaee.bbs.params.BizOutMap;
import cc.javaee.bbs.params.WebParams;
import cc.javaee.bbs.service.activity.ActivityService;
import cc.javaee.bbs.tool.FileUpload;
import cc.javaee.bbs.tool.PublicStatic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/*
 * 后台活动管理页面
 * 
 */
@Controller
@RequestMapping("/admin/activity")
public class AdminActivityController {

    @Autowired
    ActivityService activityService;

    //跳转首页
    @RequestMapping("index.do")
    public String add(HttpServletRequest request, Model model, KbrActivity activity, PageBean<KbrActivity> page) {
        String name = activity.getName();
        String startTimeStr = activity.getStartTimeStr();
        String endTimeStr = activity.getEndTimeStr();
        Integer status = activity.getStatus();

        page = activityService.findpage(activity, page);
        model.addAttribute("page", page);
        model.addAttribute("name", name);
        model.addAttribute("startTimeStr", startTimeStr);
        model.addAttribute("endTimeStr", endTimeStr);
        model.addAttribute("status", status);
        return "jsp/admin/activity/index";
    }

    @RequestMapping("infor.do")
    public String infor(HttpServletRequest request, Model model, KbrActivity activity) {
        activity = activityService.findById(activity);
        model.addAttribute("activity", activity);
        return "jsp/admin/activity/infor";
    }

    //跳转到修改页面
    @RequestMapping("toupdateoradd.do")
    public String toupdateoradd(HttpServletRequest request, Bankuai bankuai, Model model) {

        String flag = request.getParameter("flag");
        if (flag != null && flag.equals("1")) {//说明是修改

        } else if (flag != null && flag.equals("2")) {//说明是从新发布

        }

        model.addAttribute("page", null);

        return "jsp/admin/activity/updateoradd";
    }


    //修改或者新增
    @RequestMapping("updateoradd.do")
    public String updateoradd(
            HttpServletRequest request,
            KbrActivity activity,
            @RequestParam("prizeName") String[] prizeName,
            @RequestParam("prizeCount") String[] prizeCount,
            @RequestParam("prizeContent") String[] prizeContent,
            @RequestParam("arrangeName") String[] arrangeName,
            @RequestParam("arrangeStartTime") String[] arrangeStartTime,
            @RequestParam("arrangeEndTime") String[] arrangeEndTime,
//            @RequestParam("actImg") String[] actImg,
//            @RequestParam("adImg") String[] adImg
            @RequestParam(value = "actFile", required = false) MultipartFile[] actFile,
            @RequestParam(value = "adFile", required = false) MultipartFile[] adFile
    ) throws IOException {
        String[] actImg = null;
        if (actFile != null && actFile.length > 0) {
            actImg = new String[actFile.length];
            for (int i = 0; i < actFile.length; i++) {
                MultipartFile file = actFile[i];
                actImg[i] = FileUpload.upload(file);
            }
        }
        String[] adImg = null;
        if (adFile != null && adFile.length > 0) {
            adImg = new String[adFile.length];
            for (int i = 0; i < adFile.length; i++) {
                MultipartFile file = adFile[i];
                adImg[i] = FileUpload.upload(file);
            }
        }

        BizOutMap out = new BizOutMap();
        WebParams params = new WebParams();

        if (Constants.KOBORO_ACTIVITY_TYPE.equals(activity.getType())) {
            activity.setBankuaiId(Constants.KOBORO_ACTIVE_BANKUAIID);
        } else {
            activity.setBankuaiId(Constants.COMPANY_ACTIVE_BANKUAIID);
        }
        User user = (User) request.getSession().getAttribute(PublicStatic.USER);
        activity.setCreator(user.getId());
        params.put("activity", activity);
        params.put("prizeName", prizeName);
        params.put("prizeCount", prizeCount);
        params.put("prizeContent", prizeContent);

        params.put("arrangeName", arrangeName);
        params.put("arrangeStartTime", arrangeStartTime);
        params.put("arrangeEndTime", arrangeEndTime);
        params.put("actImg", actImg);
        params.put("adImg", adImg);
        params.put("userId", user.getId());
        out = activityService.saveActivity(params);
        return "redirect:index.do ";
    }

    //从新发起该活动
    @RequestMapping("rePublishAct.do")
    public String rePublishAct(HttpServletRequest request, Model model, KbrActivity activity) {
        activity = activityService.findById(activity);
        activity.setId(null);
        model.addAttribute("activity", activity);
        return "jsp/admin/activity/updateoradd";
    }

    //从新发起该活动
    @RequestMapping("updateAct.do")
    public String updateAct(HttpServletRequest request, Model model, KbrActivity activity) {
        activity = activityService.findById(activity);
        model.addAttribute("activity", activity);
        return "jsp/admin/activity/updateoradd";
    }

    //修改活动状态
    @ResponseBody
    @RequestMapping("updateStatus111.do")
    public BizOutMap updateStatus(HttpServletRequest request, KbrActivity activity) {
        BizOutMap out = new BizOutMap();
        String status = request.getParameter("status");
        String id = request.getParameter("id");
        out = activityService.updateActivity(activity);
        return out;
    }

    //删除
    @ResponseBody
    @RequestMapping("updateStatus.do")
    public BizOutMap del(int id) {
        BizOutMap out = new BizOutMap();
        KbrActivity activity = new KbrActivity();
        activity.setId(id);
        activity.setStatus(4);
        out = activityService.updateActivity(activity);
        return out;
    }
}
